/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.spring.jdbc;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author ashwin
 * 
 */
public class MapObject {
	private static final transient Log logger = LogFactory
			.getLog(MapObject.class);
	private String key = "";
	private String value = null;
	private Object actualValue = null;
	private String columnType = "";
	private int rowNum = -999;
	private List<MapObject> subGroupData = new ArrayList<MapObject>();

	/**
	 * @return the columnType
	 */
	public String getColumnType() {
		return columnType;
	}

	/**
	 * @param columnType
	 *            the columnType to set
	 */
	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the actualValue
	 */
	public Object getActualValue() {
		return actualValue;
	}

	/**
	 * @return
	 */
	public String getEvaluatedValue() {
		if (value == null) {
			return value;
		}

		if (value.equalsIgnoreCase(" ")) {
			return " ";
		}

		return value;

	}

	/**
	 * Incase the Value is a Date then Check for minimum bounds as sql server
	 * wont recognize dates less that 1753
	 * 
	 * @param actualValue
	 *            the actualValue to set
	 */
	public void setActualValue(Object actualValue) {
		this.actualValue = actualValue;
		
		/**
		 * Fix for oracle Timestamp
		 */
		if(this.actualValue != null && StringUtils.equalsIgnoreCase(actualValue.getClass().getName(),"oracle.sql.TIMESTAMP")){
			oracle.sql.TIMESTAMP timestamp = (oracle.sql.TIMESTAMP)actualValue;
			try {
				this.actualValue = timestamp.dateValue();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (this.actualValue != null && this.actualValue instanceof Date) {
			try {
				Calendar cal = new GregorianCalendar();
				cal.setTime((Date) this.actualValue);
				if (cal.get(Calendar.YEAR) < 1753) {
					cal.set(Calendar.YEAR, 1753);
					actualValue = cal.getTime();
					logger.debug("DateValue Changed  For :" + getKey()
							+ " which has value " + this.actualValue);
				}
				this.value = getFormattedDate("yyyy-MM-dd HH:mm:ss",
						(Date) this.actualValue);
				// this.value = getFormattedDate("MM/dd/yyyy", (Date)
				// actualValue);
			} catch (Exception e) {
				e.printStackTrace();
				this.value = null;
			}
		}
	}

	public static String getFormattedDate(String format, Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String newDateString = formatter.format(date);
		return newDateString;
	}

	public static void main(String[] args) {
		System.out.println(getFormattedDate("MM/dd/yyyy HH:mm:ss", new Date()));
	}

	public String getDateValue() {
		if (actualValue != null && actualValue instanceof Date) {
			try {
				// return getFormattedDate("yyyy-MM-dd", (Date) actualValue);
				return this.value;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "NULL";
	}

	/**
	 * @return the rowNum
	 */
	public int getRowNum() {
		return rowNum;
	}

	/**
	 * @param rowNum
	 *            the rowNum to set
	 */
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	/**
	 * @return the subGroupData
	 */
	public List<MapObject> getSubGroupData() {
		return subGroupData;
	}

	/**
	 * @param subGroupData
	 *            the subGroupData to set
	 */
	public void setSubGroupData(List<MapObject> subGroupData) {
		this.subGroupData = subGroupData;
	}

}
